package top.leyi.handle;

import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import top.leyi.IService.ICourseService;
import top.leyi.ServiceImpl.AuthServiceImpl;
import top.leyi.entity.Course;
import top.leyi.exception.ErrorFormatException;
import top.leyi.exception.NoSuchAuthException;
import top.leyi.utils.Result;
import top.leyi.utils.ResultGenerator;
import top.leyi.utils.WeekDateUtil;

import java.util.Map;


@RestController
@RequestMapping("/api")
public class CourseHandle {

    @Autowired
    private ICourseService courseService;

    /**
     * 获取班级的课表，返回list
     * @param classId
     * @return
     * @throws NoSuchAuthException
     */
    @RequestMapping(value = "/course/{classId}", method = RequestMethod.GET)
    public Result getCourse(@PathVariable("classId") String classId,
                            @RequestParam(value = "week",required = false) Integer week)throws NoSuchAuthException{
        if(week == null){
            week = (int)WeekDateUtil.calWeek();
        }
        return new Result(200, week+"",courseService.getCourses(classId, week) );
    }

    /**
    @desc 添加课表
     */
    //@RequestMapping(value = "/course/{classId}", method = RequestMethod.POST)
    public Result addCourse(@PathVariable("classId") String classId,
                                 @RequestParam(value = "xqj",required = true) String xqj,
                                 @RequestParam(value = "djj",required = true) String djj,
                                 @RequestParam(value = "dsz",required = true) String dsz,
                                 @RequestParam(value = "qsz",required = true) String qsz,
                                 @RequestParam(value = "jsz",required = true) String jsz,
                                 @RequestParam(value = "teacher",required = true) String teacher,
                                 @RequestParam(value = "room",required = true) String room,
                                 @RequestParam(value = "name",required = true) String name,
                                 @RequestParam(value = "owner",required = true) String owner
                                                 )throws NoSuchAuthException{

        Course course = new Course();

        course.setClassId(classId);
        course.setXqj(xqj);
        course.setDjj(djj);
        course.setDsz(dsz);
        course.setQsz(qsz);
        course.setJsz(jsz);
        course.setTeacher(teacher);
        course.setRoom(room);
        course.setName(name);
        course.setOwner(owner);
        courseService.addCourse(course);
        return ResultGenerator.OK.getResult();
    }

    /**
     @desc 添加课表
     */
    @RequestMapping(value = "/course/{classId}", method = RequestMethod.POST)
    public Result addCourses(@PathVariable("classId") String classId,
                             @RequestParam("userId") String userId,
                             @RequestParam(value = "data",required = true) String data
                            )throws NoSuchAuthException, ErrorFormatException{

        Course course = new Course();
        course.setClassId(classId);
        course.setOwner(userId);
        try{
            courseService.addCourses(course, new JSONArray(data));
        }catch (Exception e){
            e.printStackTrace();
            throw new ErrorFormatException();
        }
        return ResultGenerator.OK.getResult();
    }

    /**
     * @desc 查询当前周
     */
    @RequestMapping(value = "/course/week", method = RequestMethod.GET)
    public Result queryWeek(@RequestParam("token") String token) {

       return new Result(200, WeekDateUtil.calWeek());
    }
}
